package com.sillyhat.admin.sqlcreated.utils;

import com.sillyhat.common.utils.DBUtils;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${XUSHIKUAN} on 2017-01-08.
 */
public class SqlCreatedUtils {

    private static Logger logger = Logger.getLogger(SqlCreatedUtils.class);


    private static StringBuffer readText(String templatePathName,String[] templateContent){
        try {
            StringBuffer fileContent = new StringBuffer();
            List lines  = FileUtils.readLines(new File(templatePathName));
            List resultLines = new ArrayList();
            resultLines = new ArrayList();
            for (Object obj : lines) {
                String line = (String)obj;
                resultLines.add(MessageFormat.format(line, templateContent));
            }
            for (int i = 0; i < resultLines.size(); i++) {
                fileContent.append(resultLines.get(i));
            }
            return fileContent;
        } catch (IOException e) {
            logger.error("读取TXT文件出现异常",e);
            return null;
        }
    }

    public static String getSelectTableSql(String tableName){
        String templatePathName = System.getProperty("user.dir") + "\\SillyHatStudyLibrary-admin\\src\\main\\java\\com\\sillyhat\\admin\\sqlcreated\\utils\\selectTable.txt";
        String[] templateContent = new String[] {"'" + tableName + "'"};
        StringBuffer sql = readText(templatePathName,templateContent);
        return sql != null ? sql.toString() : "";
    }

    public static String getEntityFieldByTableField(String tableField){
        String field = "";
        String [] src = tableField.split("_");
        for (int i = 0; i < src.length; i++) {
            if(i == 0){
                field += src[i].toLowerCase();
            }else{
                field += src[i].substring(0, 1).toUpperCase() + src[i].substring(1, src[i].length()).toLowerCase();
            }
        }
        return field;
    }

    public static List<Map<String,Object>> queryList(Connection conn,String sql){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            PreparedStatement pstam = conn.prepareStatement(sql);
            ResultSet rs = pstam.executeQuery();// 执行sql语句
            // 用于获取列数、或者列类型
            ResultSetMetaData meta = rs.getMetaData();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<String, Object>();
                for (int i = 1; i <= meta.getColumnCount(); i++) {
                    String key = meta.getColumnName(i);//当前列名
                    Object value = rs.getObject(i);
                    map.put(key, value);
                }
                list.add(map);
            }
            DBUtils.close(null, pstam, rs);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error("queryList error",e);
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
        System.out.println(SqlCreatedUtils.class.getClassLoader().getResource("SqlCreatedUtils.class"));
        System.out.println(SqlCreatedUtils.class.getResource("/"));
    }

}
